package controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import pojo.Address;
import pojo.Restaurant;
import pojo.Subscribe;
import pojo.User;
import service.IMainService;
import service.IUserService;
import utils.Common;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {
    private Map<String, Object> responseMsg = new HashMap<>();

    @Resource
    private IUserService userService;

    @Resource
    private IMainService mainService;
    @Resource
    private MainController mainController;
    // 工具类
    private Common common;

    /**
     * 修改信息
     *
     * @param user 修改用户参数
     * @return 修改结果
     */
    @RequestMapping("/reset")
    @ResponseBody
    public Map<String, Object> reset(@RequestBody User user) {
        int result = userService.reset(user);
        User user1 = mainService.getUserById(user.getId());
        Map<String, Object> resetMsg = new HashMap<>();
        if (result > 0) {
            // 修改成功
            resetMsg.put("success", true);
            resetMsg.put("user", user1);
        } else {
            // 修改失败
            resetMsg.put("success", false);
        }
        return resetMsg;
    }

    /**
     * 余额充值
     *
     * @param id    用户id
     * @param value 充值金额
     * @return 充值结果, 刷新用户数据
     */
    @RequestMapping("/recharge")
    @ResponseBody
    public Map<String, Object> recharge(@RequestParam Integer id, @RequestParam BigDecimal value) {

        int result = userService.recharge(id, value);
        User user1 = mainService.getUserById(id);
        Map<String, Object> resetMsg = new HashMap<>();
        if (result > 0) {
            resetMsg.put("success", true);
            resetMsg.put("message", "recharge success");
            resetMsg.put("money", user1.getWallet());
        } else {
            resetMsg.put("success", false);
            resetMsg.put("message", "recharge failure");
        }
        return resetMsg;
    }

    /**
     * 获取address列表
     *
     * @param id 用户id
     * @return 地址列表
     */
    @RequestMapping("/address")
    @ResponseBody
    public Map<String, Object> getAddressList(@RequestParam Integer id) {
        List<Address> list = userService.getAddressList(id);
        Map<String, Object> addressMsg = new HashMap<>();
        if (!list.isEmpty()) {
            addressMsg.put("success", true);
            addressMsg.put("address_list", list);
        } else {
            addressMsg.put("success", false);
        }
        return addressMsg;
    }

    /**
     * 新增收货地址
     *
     * @param address 地址对象
     * @return 新增结果
     */
    @RequestMapping("/newaddress")
    @ResponseBody
    public Map<String, Object> newAddress(@RequestBody Address address) {
        int result = userService.newAddress(address);
        Map<String, Object> addMsg = new HashMap<>();
        if (result > 0) {
            addMsg.put("success", true);
            addMsg.put("message", "add new address success");
        } else {
            addMsg.put("success", false);
            addMsg.put("message", "add new address failure");
        }
        return addMsg;
    }

    /**
     * 更新地址信息
     *
     * @param address 地址
     * @return 更新结果
     */
    @RequestMapping("/updateaddress")
    @ResponseBody
    public Map<String, Object> updateAddress(@RequestBody Address address) {
        int result = userService.updateAddress(address);
        if (result > 0) {
            responseMsg.put("success", true);
            responseMsg.put("message", "update address success");
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "update address failure");
        }
        return responseMsg;
    }

    /**
     * 设置默认地址
     *
     * @param addId 地址id
     * @return 设置结果
     */
    @RequestMapping("/defaultadd")
    @ResponseBody
    public Map<String, Object> updateDefaultAddress(@RequestParam Integer addId) {
        int result = userService.setDefaultAddress(addId);
        if (result > 0) {
            responseMsg.put("success", true);
            responseMsg.put("message", "set default address success");
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "set default address failure");
        }
        return responseMsg;
    }

    /**
     * 删除收货地址
     *
     * @param addId 地址id
     * @return 删除结果
     */
    @RequestMapping("/address/delete")
    @ResponseBody
    public Map<String, Object> deleteAddress(@RequestParam Integer addId) {
        int result = userService.deleteAddress(addId);
        if (result > 0) {
            responseMsg.put("success", true);
            responseMsg.put("message", "delete address success");
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "delete address failure");
        }
        return responseMsg;
    }

    /**
     * 收藏店铺
     *
     * @param subscribe 收藏对象
     * @return 收藏结果
     */
    @RequestMapping("/subscribe")
    @ResponseBody
    public Map<String, Object> subscribe(@RequestBody Subscribe subscribe) {
        responseMsg.clear();

        subscribe.setCreateTime(new Date());
        System.out.println(subscribe);
        int result = userService.subscribeRest(subscribe);
        if (result > 0) {
            responseMsg.put("success", true);
            responseMsg.put("message", "subscribe restaurant success");
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "subscribe restaurant failure");
        }
        return responseMsg;
    }

    @RequestMapping("/cancelsub")
    @ResponseBody
    public Map<String, Object> cancelNotify(@RequestBody Subscribe subscribe) {
        responseMsg.clear();
        int result = userService.cancelSubscribe(subscribe);
        if (result > 0) {
            responseMsg.put("success", true);
            responseMsg.put("message", "cancel notify restaurant success");
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "cancel notify restaurant failure");
        }
        return responseMsg;
    }

    /**
     * 获取我的收藏店铺列表
     *
     * @param uid 用户id
     * @return 收藏列表
     */
    @RequestMapping("/mycollection")
    @ResponseBody
    public Map<String, Object> myCollections(
            @RequestParam Integer uid
            , @RequestParam String query,
            @RequestParam int pagenum,
            @RequestParam int pagesize) {
        responseMsg.clear();
        Map<String, Object> map = new HashMap<>();
        map.put("query", '%' + query + '%');
        map.put("pagenum", (pagenum - 1) * pagesize);
        map.put("pagesize", pagesize);
        map.put("userId", uid);
        List<Restaurant> list = userService.getMyCollections(map);
        int total = userService.selectSubscribeTotal(map);
        if (!list.isEmpty()) {
            responseMsg.put("success", true);
            responseMsg.put("message", "get my collection success");
            responseMsg.put("my_collection_list", list);
            responseMsg.put("total", total);
        } else {
            responseMsg.put("success", false);
            responseMsg.put("message", "get my collection failure");
        }
        return responseMsg;
    }

    /**
     * 保存照片
     *
     * @return 修改结果
     */
    @RequestMapping("/savePhoto")
    @ResponseBody
    public Map<String, Object> savePhoto(@RequestParam int userId, @RequestParam("file") MultipartFile multipartFile, HttpServletRequest request) {
        String photo = mainController.addImg(multipartFile, request);
        User user = new User();
        user.setPhoto(photo);
        user.setId(userId);
        int result = userService.reset(user);
        user = mainService.getUserById(userId);
        user.setPhoto(photo);
        Map<String, Object> resetMsg = new HashMap<>();
        if (result > 0) {
            // 修改成功
            resetMsg.put("success", true);
            resetMsg.put("photo", photo);
            resetMsg.put("user",user);
        } else {
            // 修改失败
            resetMsg.put("success", false);
        }
        return resetMsg;
    }
}
